//
//  HomeViewController.m
//  AgriMobile
//
//  Created by 春名 啓作 on 11/05/14.
//  Copyright 2011 e-agri. All rights reserved.
//

#import "HomeViewController.h"

@implementation HomeViewController

@synthesize homeTableView;
@synthesize postsInSection;
@synthesize sectionHeaders;
@synthesize topicId;

NSDictionary *maps = nil;
NSString *sessId = nil;
//HttpMethod *http = nil;

#pragma mark -
#pragma mark Initialization

/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];

	NSLog(@"HomeView didLoad");
	/*
	UIBarButtonItem *button = [[[UIBarButtonItem alloc]
								initWithTitle:@"更新"
								style:UIBarButtonItemStyleBordered
								target:self
								action:@selector(clickUpdate:)] autorelease];
	self.navigationItem.rightBarButtonItem = button;
	 */

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

}

- (void)viewWillAppear:(BOOL)animated {

	NSLog(@"HomeViewController - viewWillAppear, topicId = %@", topicId);

	SharedDictionary *sd = [SharedDictionary sharedDictionary];
	self.postsInSection = [[[sd.data objectForKey:@"top"] objectForKey:@"posts"] objectForKey:@"data"];
	self.sectionHeaders = [[sd.data objectForKey:@"top"] objectForKey:@"sectionHeaders"];
	
	sessId = [[sd.data objectForKey:@"top"] objectForKey:@"sessid"];
	NSLog(@"sessid = %@", sessId);
	
	NSLog(@"postsInSection = %@", postsInSection);
	NSLog(@"sectionHeaders = %@", sectionHeaders);
	NSLog(@"sessId = %@", sessId);

	[self.tableView reloadData];
}

/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.

    return [self.postsInSection count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
	
	return [[self.postsInSection objectForKey:[[NSNumber numberWithInt:section] stringValue]] count];
}


/*
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger) section {

	return [self.sectionHeaders objectForKey:[[NSNumber numberWithInt:section] stringValue]];
}
 */

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
	//cell.textLabel.text = [NSString stringWithFormat:@"%d-%d", indexPath.section, indexPath.row];
	NSLog(@"in section %d row %d", indexPath.section, indexPath.row);
	CGRect rect = cell.frame;
	CGPoint offset =  tableView.contentOffset;  
	rect.origin.x = rect.origin.x - offset.x;
	rect.origin.y = rect.origin.y - offset.y;
	NSLog(@"scrolled x:%d y:%d", rect.origin.x, rect.origin.y);
	
	NSArray *posts = [self.postsInSection objectForKey:[[NSNumber numberWithInt:indexPath.section] stringValue]];
	NSArray *onePost = [posts objectAtIndex:indexPath.row];
	
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	
	cell.textLabel.font = [UIFont systemFontOfSize:10.0];
	cell.textLabel.text = [NSString stringWithFormat:@"%@  %@",
						   [onePost objectAtIndex:0],[onePost objectAtIndex:3]];
	
	//cell.detailTextLabel.font = [UIFont systemFontOfSize:8.0];
	cell.detailTextLabel.text = [onePost objectAtIndex:2];
	cell.detailTextLabel.textColor = [UIColor blueColor];
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    /*
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
    */

	TopicViewController *topicViewController = [[TopicViewController alloc] initWithNibName:@"TopicView" bundle:nil];
	
	NSArray *posts = [self.postsInSection objectForKey:[[NSNumber numberWithInt:indexPath.section] stringValue]];
	NSArray *onePost = [posts objectAtIndex:indexPath.row];
	topicViewController.topicId = [onePost objectAtIndex:1];
	
	[self.navigationController pushViewController:topicViewController animated:YES];
	[topicViewController release];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

	//UIView *headerView = [[[HeaderView alloc] init] autorelease];
	UIView *headerView = [[[UIView alloc] init] autorelease];
	UIColor *colorG = HEXCOLOR(0xc9e6b7);
	headerView.backgroundColor = colorG;
	
	UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 22.0f)];
	lbl.backgroundColor = [UIColor clearColor];
	lbl.textColor = [UIColor blackColor];
	lbl.font = [UIFont systemFontOfSize:16];
	//lbl.text = [NSString stringWithFormat:[TYPE_ARRAY objectAtIndex:section]];
	lbl.text = [self.sectionHeaders objectForKey:[[NSNumber numberWithInt:section] stringValue]];
	[headerView addSubview:lbl];
	[lbl release];

	return headerView;
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
	NSLog(@"HomeView - didReceiveMemoryWarning");
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
	NSLog(@"HomeView - viewDidUnload");
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
	NSLog(@"HomeView - dealloc");

	[sectionHeaders release];
	[postsInSection release];
	[maps release];
	//[http release];
	[sessId release];

    [super dealloc];
}


@end

